// pages/shop_cart/shop_cart.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cart_list: [],
    isallcheck: true,
    isdelete: false,
    state: [],
    isallcheckstate: false,
    allprice: 0,
    isload: false
  },
  checkboxChange: function (e) {
    var cart_list = this.data.cart_list, value = e.detail.value
    for (var i = 0; i < cart_list.length; i++) {
      cart_list[i].checked = false
      for (var j = 0; j < value.length; j++) {
        if (cart_list[i].value == value[j]) {
          cart_list[i].checked = true
          break
        }
      }
    }
    this.setData({
      cart_list: cart_list,
      isallcheck: this.isallcheck()
    })
    this.all_price()
  },
  isallcheck: function () {
    var j = 0;
    var cart_list = this.data.cart_list
    for (var i in cart_list) {
      if (cart_list[i].checked) {
        j++
      }
    }
    if (j == cart_list.length) {
      return true
    } else {
      return false
    }
  },
  allcheck: function () {
    var cart_list = this.data.cart_list
    if (this.isallcheck()) {
      for (var i in cart_list) {
        cart_list[i].checked = false
      }
      this.setData({
        isallcheck: false
      })
    } else {
      for (var i in cart_list) {
        cart_list[i].checked = true
      }
      this.setData({
        isallcheck: true
      })
    }
    this.setData({
      cart_list: cart_list
    })
    this.all_price()
  },
  eitor: function () {
    var cart_list = this.data.cart_list
    this.setData({
      state: cart_list,
      isallcheckstate: this.data.isallcheck
    })
    for (var i in cart_list) {
      cart_list[i].checked = false
    }
    this.setData({
      isdelete: true,
      cart_list: cart_list,
      isallcheck: this.isallcheck(),
      allprice: 0
    })
  },
  yes: function () {
    this.setData({
      isdelete: false,
      isallcheck: this.data.isallcheckstate,
      cart_list: this.data.state
    })
    this.all_price()
  },
  cut_price: function (e) {
    var index = e.target.id
    var value = e.detail.value
    var obj = {}
    var str = "cart_list[" + index + "].num"
    if (e.detail.value != "") {
      obj[str] = parseFloat(value)
      this.setData(obj)
      this.all_price()
    }
  },
  onset: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id
    var specval_id = e.currentTarget.dataset.specval_id
    var index = e.currentTarget.id
    var that = this
    var cart_list = this.data.cart_list
    if (e.detail.value == "") {
      cart_list[index].num = 1
      this.setData({
        cart_list: cart_list
      })
    }
    app.ajax({
      url: app.host + "mall/cart/edit",
      data: {
        goods_id: goods_id,
        specval_id: specval_id,
        num: cart_list[index].num
      }
    })
  },
  add: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id
    var specval_id = e.currentTarget.dataset.specval_id
    var index = e.currentTarget.id
    var obj = {}
    var that = this
    var str = "cart_list[" + index + "].num"
    var loading = "cart_list[" + index + "].loading"
    obj[str] = parseInt(this.data.cart_list[index].num) + 1
    obj[loading] = true
    this.setData(obj)
    app.ajax({
      url: app.host + "mall/cart/edit",
      data: {
        goods_id: goods_id,
        specval_id: specval_id,
        num: that.data.cart_list[index].num
      },
      success: function () {
        var newobj = {}
        newobj[loading] = false
        that.setData(newobj)
      }
    })
    this.all_price()
  },
  all_price: function () {
    var allprice = 0
    var cart_list = this.data.cart_list
    for (var i in cart_list) {
      if (cart_list[i].checked == true) {
        allprice = app.accAdd(allprice, app.accMul(parseFloat(cart_list[i].price), parseFloat(cart_list[i].num))).toFixed(2) 
      }
    }
    app.globalData.allprice = allprice
    this.setData({
      allprice: allprice
    })
  },
  reduce: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id
    var specval_id = e.currentTarget.dataset.specval_id
    var that = this
    var index = e.target.id
    var cart_list = this.data.cart_list
    var loading = "cart_list[" + index + "].loading"
    if (this.data.cart_list[index].num > 1) {
      var obj = {}
      var str = "cart_list[" + index + "].num"
      obj[str] = this.data.cart_list[index].num - 1
      obj[loading] = true
      this.setData(obj)
      app.ajax({
        url: app.host + "mall/cart/edit",
        data: {
          goods_id: goods_id,
          specval_id: specval_id,
          num: that.data.cart_list[index].num
        }, success: function () {
          var newobj = {}
          newobj[loading] = false
          that.setData(newobj)
        }
      })
    }
    this.all_price()
  },
  del: function () {
    var cart_id = []
    var del_obj = []
    var cart_list = this.data.cart_list
    var that = this
    var del_num = 0
    var state = this.data.state
    wx.showModal({
      title: "确定删除选中的商品吗？",
      content: '',
      success: function (msg) {
        if (msg.confirm) {
          for (var i in cart_list) {
            if (cart_list[i].checked == true) {
              cart_id.push(cart_list[i].cart_id)
              del_num += cart_list[i].num
            } else {
              del_obj.push(cart_list[i])
            }
          }
          for (var j = 0; j < state.length; j++) {
            for (var k = 0; k < cart_id.length; k++) {
              if (state[j].cart_id == cart_id[k]) {
                cart_list.splice(j, 1)
                state.splice(j, 1)
                j--
                break
              }
            }
          }
          that.all_price();
          that.setData({
            state: state
          })
          app.ajax({
            url: app.host + "mall/cart/del",
            data: {
              cart_id: cart_id.join(',')
            },
            success: function () {
              that.setData({
                cart_list: del_obj
              })
              app.globalData.cart_num = app.globalData.cart_num - del_num
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  go_pay: function () {
    var order_data = {}
    order_data.allprice = this.data.allprice
    order_data.num = 0
    var specval = []
    var cart_list = this.data.cart_list
    for (var i in cart_list) {
      if (cart_list[i].checked == true) {
        var obj = {}
        obj.specval_id = cart_list[i].specval_id
        obj.num = cart_list[i].num
        order_data.num += parseInt(cart_list[i].num)
        specval.push(obj)
      }
    }
    order_data.specval = specval
    let order_dataStr = JSON.stringify(order_data);
    if (order_data.num) {
      wx.redirectTo({
        url: '/pages/pay_page/pay_page?order_data=' + order_dataStr,
      })
    } else {
      wx.showToast({
        title: '你还没有选择商品哦',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.ajax({
      url: app.host + "mall/Freqlist/recgoods",
      success:function(res){
        if(res.data.code==1){
            wx.showLoading({
              title: '加载中',
            })
            app.ajax({
              url: app.host + "mall/cart",
              success: function (msg) {
                wx.hideLoading()
                wx.stopPullDownRefresh()
                var data = msg.data.data
                var allprice = 0
                for (var i in data) {
                  data[i].checked = true
                  data[i].value = i
                  data[i].loading = false
                }
                that.setData({
                  cart_list: data,
                  allprice: allprice,
                  isload: true
                })
                that.all_price()
              }
            })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})